package com.zz.webapp.manager;

import com.zz.webapp.dao.UserDao;
import com.zz.webapp.dao.UserProfileDao;
import com.zz.webapp.dto.UserDto;
import com.zz.webapp.enums.ErrorCode;
import com.zz.webapp.model.User;
import com.zz.webapp.model.UserProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2017/2/1.
 */
@Component
public class UserManager {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserProfileDao userProfileDao;

    @Transactional
    public void createUser(UserDto userDto) throws Exception {
        User user = new User(userDto);
        if(Boolean.valueOf(1 != userDao.create(user))){
            throw new Exception(ErrorCode.CREATE_USER_FAILED.toString());
        }
        UserProfile userProfile = new UserProfile(userDto,user);
        if(Boolean.valueOf(1 != userProfileDao.create(userProfile))){
            throw  new Exception(ErrorCode.CREATE_USER_FAILED.toString());
        }
    }
}
